package com.handsoninfo;

import net.sourceforge.tess4j.ITessAPI;
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.Word;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.List;

public class Ui extends JPanel implements MouseMotionListener {

    final DecimalFormat df = new DecimalFormat("#0.0");
    final BufferedImage bi;

    final List<Word> words;

    final Division divisions;

    final JFrame frame;

    public Ui(final BufferedImage bi, final List<Word> words, final Division divisions) {
        super();
        this.bi = bi;
        this.words = words;
        this.divisions = divisions;
        this.frame = new JFrame();
        init();
    }

    public Ui(Recognition.Instance instance) {
        this(instance.getBi(), instance.getWords(), instance.getDivisions());
    }

    private void init() {
        addMouseMotionListener(this);
        // create a JFrame to display the image
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.add(this);
        frame.setSize(bi.getWidth() + 17, bi.getHeight() + 40);

//        frame.pack(); // 计算并设置 JFrame 大小
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
//        System.out.println(frame.getContentPane().getBounds());
    }

    @Override
    public void mouseDragged(MouseEvent e) {

    }

    @Override
    public void mouseMoved(MouseEvent e) {
        int x = e.getX();
        int y = e.getY();
        Rectangle rect = frame.getContentPane().getBounds();
        frame.setTitle("(" + x + ", " + y + ")" + rect);
    }

    @Override
    public void paintComponent(Graphics g) {

        super.paintComponent(g);
        g.drawImage(bi, 0, 0, null);
        g.setColor(Color.RED);
        Font font = new Font("Serif", Font.BOLD, 26);
        g.setFont(font);
        for (Word word : words) {
            Rectangle rect = word.getBoundingBox();
//            if ("莫".equals(word.getText())) g.fillRect(rect.x, rect.y, rect.width, rect.height);
            if (!divisions.containsWord(word)) continue;
            g.drawString(word.getText(), rect.x, rect.y);
        }
        for (Word word : words) {
            if (!divisions.containsWord(word)) continue;
            Rectangle rect = word.getBoundingBox();
            Color color = Utils.getTextColor(bi, word);
            assert(null != color); // `divisions.containsWord(word)`这个判断已经检查过
            g.setColor(color);
            g.drawRect(rect.x, rect.y, rect.width, rect.height);
        }
        g.setColor(Color.BLACK);
        font = new Font("Serif", Font.BOLD, 12);

        g.setFont(font);
        for (Word word : words) {
            if (!divisions.containsWord(word)) continue;
            Rectangle rect = word.getBoundingBox();
            g.drawString(df.format(word.getConfidence()) + "%", rect.x, rect.y);
        }

        g.setColor(Color.GREEN);
        for (Division.Block block : divisions.getBlocks()) {
            Rectangle rect = block.getRect();
            g.drawRect(rect.x, rect.y, rect.width, rect.height);
        }
    }
}
